package com.yesep.learn.design.behavioral.template_method;

import com.yesep.learn.design.common.pojo.*;

import java.util.*;

public abstract class ShoppingOrder {

    private List<Goods> goodsList;

    public ShoppingOrder(List<Goods> goodsList) {
        this.goodsList = goodsList;
    }

    public final void checkout() {
        //1、检查库存
        boolean success = checkInventory();
        if (!success) {
            throw new RuntimeException("Insufficient inventory of goods");
        }
        //2、计算总价
        double totalPrice = calculateTotalPrice();
        //3、获取折扣
        double promotion = getPromotion();
        //生成定单
        generateOrder(totalPrice * promotion);
    }

    /**
     * 生成定单
     */
    protected void generateOrder(double totalPrice) {
        System.out.printf("Generate Order total price %s NO %s.\n", totalPrice, UUID.randomUUID());
    }

    /**
     * 折扣比率
     */
    protected double getPromotion() {
        System.out.println("No promotion applied.");
        return 1;
    }

    /**
     * 计算总价
     */
    protected double calculateTotalPrice() {
        double totalPrice = goodsList.stream().mapToDouble(Goods::getPrice).sum();
        System.out.printf("Total Price : %s\n", totalPrice);
        return totalPrice;
    }

    /**
     * 检查库存
     */
    protected boolean checkInventory() {
        System.out.println("Adequate inventory of goods.");
        return true;
    }
}
